package com.ax.base.drugproducer.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.base.common.utils.PinYinUtil;
import com.ax.base.drugproducer.dao.BaseProducerMapper;
import com.ax.base.drugproducer.domain.BaseProducer;
import com.ax.base.drugproducer.service.DrugProducerService;

@Service
public class DrugProducerServiceImpl implements DrugProducerService {

	@Autowired
	BaseProducerMapper baseProducerMapper;
	
	/**
	 * 药品厂家
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@Override
	public List<BaseProducer> queryDrugproducerSer(Map<String, Object> params) {
	//	Query query = new Query(params);
		List<BaseProducer> baseDrugManagers = baseProducerMapper.queryDrugproducerMapper(params);
	//	int total = baseProducerMapper.count(query);
	//	PageUtils pageUtil = new PageUtils(baseDrugManagers,total);
		return baseDrugManagers;
	}

	/**
	 * 药品厂家增加(模块:药品厂家)
	 * 
	 * @param BaseProducer baseProducer
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertBaseDrugSer(BaseProducer baseProducer) {
		baseProducer.setFirstLetter(PinYinUtil.getFirstSpell(baseProducer.getProducerName()));
		baseProducerMapper.insertSelective(baseProducer);
	}

	/**
	 * 药品厂家修改(模块:药品厂家)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return HTML
	 */
	@Override
	public BaseProducer getProducerSer(Long id) {
		BaseProducer baseProducer = baseProducerMapper.getProducerMapper(id);
		String[] dz = baseProducer.getAreaCode().split(",");
		baseProducer.setProvince(dz[0]);
		baseProducer.setCity(dz[1]);
		baseProducer.setVillage(dz[2]);
		return baseProducer;
	}

	/**
	 * 药品厂家修改(模块:药品厂家)
	 * 
	 * @param BaseProducer baseProducer
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateBaseDrugSer(BaseProducer baseProducer) {
		baseProducer.setFirstLetter(PinYinUtil.getFirstSpell(baseProducer.getProducerName()));
		baseProducerMapper.updateBaseDrugMapper(baseProducer);
	}

	/**
	 * 验证信用代码是否重复
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@Override
	public boolean validateSer(Map<String,Object> params) {
		boolean validate;
		validate = baseProducerMapper.queryDrugproducerMapper(params).size() > 0;
		return validate;
//		Map<String,Object> map = new HashMap<String, Object>();
//		map.put("producerName",producerName);
//		String json="{\"message\": \"\",\"value\": [";
//			List<BaseProducer> list = baseProducerMapper.queryDrugproducerMapper(map);
//		if(list != null && !list.isEmpty()){
//				for(int i=0;i<list.size();i++){
//					json+="{"+"\"userName\":"+"\""+list.get(i).getProducerName()+"\"" +"},";
//				}
//				json = json.substring(0,json.length()-1>0?json.length()-1:1); 
//				json+="],\"code\": 200,\"redirect\": \"\"}";
//				return json;
//			}else{
//				json+="],\"code\": 400,\"redirect\": \"\"}";
//				return json;
//			}
		//"userId": 20000
	}
	
	/**
	 * 验证信用代码是否重复(修改)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@Override
	public boolean updateValidateSer(String userName) {
		Map<String,Object> params = new HashMap<String,Object>();
	    String code = userName.substring(0,userName.indexOf("///"));
		String id = userName.substring(userName.indexOf("///") + 3);
		params.put("creditCode",code);
		params.put("producerId",id);
		boolean validate;
		validate = baseProducerMapper.validateMapper(params).size() > 0;
		return validate;
	}


}
